package cn.s1995.sloth.app.controller;

import java.util.List;
import java.util.Objects;

import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.s1995.sloth.bizs.constant.BizCst;
import cn.s1995.sloth.bizs.entity.Video;
import cn.s1995.sloth.bizs.oo.vo.VideoVO;
import cn.s1995.sloth.bizs.service.VideoService;
import cn.s1995.sloth.bizs.service.common.CacheService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.security.annotation.Inner;
import cn.s1995.sloth.common.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/m/video")
@Tag(description = "Video", name = "APP-视频模块")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ApiVideoController {

	private final VideoService videoService;

	private final CacheService cacheService;

	@Autowired
	private HttpServletRequest request;

	@Inner(false)
	@ApiOperationSupport(order = 1)
	@Operation(summary = "随机获取视频", description = "随机获取视频，最多5个")
	@GetMapping("/random")
	public R<List<VideoVO>> getRandomVideo() {
		List<Video> list = videoService.getRandomList(5);
		//回显用户点赞状态
		Long uid = cacheService.getUidByToken(request.getHeader(HttpHeaders.AUTHORIZATION));
		return R.ok(videoService.setStatus(uid, list));
	}

	@Inner(false)
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页查询视频", description = "分页查询视频")
	@GetMapping("/page")
	public R<Page<VideoVO>> list(@ParameterObject Page page, @ParameterObject Video req) {
        LambdaQueryWrapper<Video> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(Objects.nonNull(req.getShopId()), Video::getShopId, req.getShopId())
				.eq(Objects.nonNull(req.getUid()), Video::getUid, req.getUid())
				.eq(Video::getIsSale, BizCst.CommonBool.yes)
				.and(
	                StrUtil.isNotEmpty(req.getSearch()),
	                w -> w.like(Video::getName, req.getSearch())
	                        .or()
	                        .like(Video::getDesc, req.getSearch()))
				.orderByDesc(Video::getCreateTime);
		Page resultPage = videoService.page(page, wrapper);
		Long uid = cacheService.getUidByToken(request.getHeader(HttpHeaders.AUTHORIZATION));
		if(ObjectUtil.isNotEmpty(resultPage.getRecords())){
			resultPage.setRecords(videoService.setStatus(uid, resultPage.getRecords()));
		}
        return R.ok(resultPage);
	}

	@ApiOperationSupport(order = 3)
	@Operation(summary = "关注店铺/用户的视频", description = "关注店铺或用户的视频")
	@GetMapping("/following")
	public R<Page<VideoVO>> following(@ParameterObject Page page) {
		Page resultPage = videoService.getFollowPage(page);
		if(ObjectUtil.isNotEmpty(resultPage.getRecords())) {
			resultPage.setRecords(videoService.setStatus(SecurityUtils.getUser().getId(), resultPage.getRecords()));
		}
        return R.ok(resultPage);
	}

	@Inner(false)
	@ApiOperationSupport(order = 4)
	@Operation(summary = "视频详情", description = "视频详情")
	@Parameters({
	   @Parameter(name = "id",description = "视频id", required = true, in = ParameterIn.PATH),
	})
	@GetMapping("/detail/{id}")
	public R<Video> getVideoDetail(@PathVariable("id") Long id) {
		Video video = videoService.getVideoAndRecommend(id);
		Long uid = cacheService.getUidByToken(request.getHeader(HttpHeaders.AUTHORIZATION));
		if(ObjectUtil.isNotNull(video)){
			video = videoService.setStatus(uid, video);
		}
		return R.ok(video);
	}

	@Inner(false)
	@ApiOperationSupport(order = 5)
    @Operation(summary = "播放视频", description = "播放视频")
	@Parameters({
	   @Parameter(name = "id",description = "视频id", required = true, in = ParameterIn.PATH),
	})
	@PostMapping("/play/{id}")
	public R playVideo(@PathVariable("id") Long id) {
		return videoService.play(id);
	}

	@ApiOperationSupport(order = 6)
    @Operation(summary = "分享视频", description = "分享视频")
	@Parameters({
	   @Parameter(name = "id",description = "视频id", required = true, in = ParameterIn.PATH),
	})
	@PostMapping("/share/{id}")
	public R shareVideo(@PathVariable("id") Long id) {
		return videoService.share(id);
	}

	@ApiOperationSupport(order = 7)
    @Operation(summary = "点赞视频", description = "点赞视频")
	@Parameters({
	   @Parameter(name = "id",description = "视频id", required = true, in = ParameterIn.PATH),
	})
	@PostMapping("/like/{id}")
	public R likeVideo(@PathVariable("id") Long id) {
		return videoService.like(id);
	}

	@ApiOperationSupport(order = 8)
    @Operation(summary = "收藏视频", description = "收藏视频")
	@Parameters({
	   @Parameter(name = "id",description = "视频id", required = true, in = ParameterIn.PATH),
	})
	@PostMapping("/collect/{id}")
	public R collectVideo(@PathVariable("id") Long id) {
		return videoService.collect(id);
	}

}
